# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/16 19:33
# @Author   : Robot-Zsj
"""
description:


i:
1
App1 1 09:00 10:00
09:30

o:
App1
"""


class App:

    def __init__(self, name, priority, start, end):
        self.name = name
        self.priority = priority
        self.start = start
        self.end = end


def convert(time):
    hours, minutes = map(int, time.split(":"))
    return hours * 60 + minutes


def hasInterSection(a, b):
    if b.start <= a.start < b.end:
        return True

    if a.start <= b.start < a.end:
        return True

    return False


n = int(input())

apps = []

for _ in range(n):
    name, priority, start, end = input().split(' ')
    apps.append(App(name, int(priority), convert(start), convert(end)))

queryTime = convert(input())


def getResult():
    registereds = []

    apps.sort(key=lambda x: -x.priority)

    for app in apps:
        if app.start >= app.end:
            continue

        conflict = False

        for registered in registereds:
            if hasInterSection(app, registered):
                conflict = True
                break

        if conflict:
            continue

        registereds.append(app)

    ans = "NA"

    for registered in registereds:
        if registered.start <= queryTime < registered.end:
            ans = registered.name
            break

    return ans


print(getResult())
